class OutsideClick {
    initialize(dotNetHelper, element, id) {
        const listener = (e) => {
            var currentPopup = document.getElementById('popovercontent-' + id);
            if (e.type == 'contextmenu' || !e.target) return;
            if (element) {
                if (e.type == 'mousedown' && !element.contains(e.target) && !currentPopup.contains(e.target)) {
                    this.closePopup(dotNetHelper, listener);
                }
            } else {
                if (!currentPopup.contains(e.target)) {
                    this.closePopup(dotNetHelper, listener);
                }
            }
        }
        document.removeEventListener('mousedown', listener);
        document.addEventListener('mousedown', listener);
    }
    closePopup(dotNetHelper, listener) {
        dotNetHelper.invokeMethodAsync("OnOutsideClick");
        document.removeEventListener('mousedown', listener);
    }
}
window.OutsideClick = new OutsideClick();